//
//  MNDPlusDetaiBottomViewController.m
//  minidai
//
//  Created by Vitas on 2018/7/13.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDPlusDetaiBottomViewController.h"
#import "MNDToolIntroduceViewController.h"

typedef enum : NSUInteger {
    kTopTypeTool = 100,
    kTopypeProblem
}kTopType;

@interface MNDPlusDetaiBottomViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollerView;
@property (weak, nonatomic) IBOutlet UIButton *toolIntroduceButton;
@property (weak, nonatomic) IBOutlet UIButton *problemButton;
@property (weak, nonatomic) IBOutlet UIView *bottonLineView;
@property(nonatomic, assign) CGRect originRect;
@property (weak, nonatomic) IBOutlet UIButton *investButton;
//为了适配iPhone X加入的view的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headerViewHeight;
//
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewHeightReduce;

@property(nonatomic, strong) MNDToolIntroduceViewController *toolVC;
@end

@implementation MNDPlusDetaiBottomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollerView.pagingEnabled = YES;
    
    [self setupUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotificationAction:) name:@"scroll_up_or_down" object:nil];
    if (kScreenHeight == 812) { //iPhone X
        self.headerViewHeight.constant = 44;
        self.scrollViewHeightReduce.constant = -158;
    }else {
        self.headerViewHeight.constant = 0;
        self.scrollViewHeightReduce.constant = -114;
    }
    // Do any additional setup after loading the view.
}

- (void)setInvestModel:(MNDPlusInvestModel *)investModel {
    _investModel = investModel;
    self.toolVC.investModel = self.investModel;
}


-(void)scrollNotificationAction:(NSNotification *)notification {
    NSInteger scrollType = [notification.object integerValue];
    if (scrollType == 1) {
        self.investButton.hidden = NO;
    }else {
        self.investButton.hidden = YES;
    }
    
}

- (void)setupUI {
    self.toolIntroduceButton.selected = YES;
    self.problemButton.selected = NO;
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.originRect = self.bottonLineView.frame;
    if (self.originRect.origin.x > kScreenWidth *0.5) {
        self.originRect = CGRectMake(self.originRect.origin.x - 93, self.originRect.origin.y, self.originRect.size.width, self.originRect.size.height);
    }
}

- (IBAction)investAction:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"scroll_up_or_down" object:@0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"bottomInvestClickNotification" object:nil];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
 
    if (scrollView.contentOffset.x > kScreenWidth*0.5) {//常见问题
        self.toolIntroduceButton.selected = NO;
        self.problemButton.selected = YES;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.bottonLineView.frame = CGRectMake(self.originRect.origin.x  + 93, self.originRect.origin.y, self.bottonLineView.bounds.size.width, self.bottonLineView.bounds.size.height);
        }];
    }else {//工具介绍
        self.toolIntroduceButton.selected = YES;
        self.problemButton.selected = NO;
        [UIView animateWithDuration:0.25 animations:^{
            self.bottonLineView.frame = CGRectMake(self.originRect.origin.x, self.originRect.origin.y, self.bottonLineView.bounds.size.width, self.bottonLineView.bounds.size.height);
        }];
    }
    NSLog(@"contentOffset.x = %lf",scrollView.contentOffset.x);
}

#pragma mark - Action

- (IBAction)topButtonAction:(UIButton *)sender {
    if (sender.tag == kTopTypeTool) {
        [self.scrollerView scrollToLeft];
        self.toolIntroduceButton.selected = YES;
        self.problemButton.selected = NO;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.bottonLineView.frame = CGRectMake(self.originRect.origin.x, self.originRect.origin.y, self.bottonLineView.bounds.size.width, self.bottonLineView.bounds.size.height);
        }];
    }else {
        [self.scrollerView scrollToRight];
        self.toolIntroduceButton.selected = NO;
        self.problemButton.selected = YES;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.bottonLineView.frame = CGRectMake(self.originRect.origin.x  + 93, self.originRect.origin.y, self.bottonLineView.bounds.size.width, self.bottonLineView.bounds.size.height);
        }];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toolVC"]) {
        self.toolVC = [segue destinationViewController];
    }else {
        
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
